<?php

namespace app\controller;

use app\BaseController;
use app\message;

class Image extends BaseController
{
    public function uploads()
    {
        if (!$this->request->isPost()) return ReJson(message::$ERROR_CODE, message::$ILLEGAL_REQUEST);

        // 接收文件上传
        $file = $this->request->file("file");
        if (!$file) {
            return ReJson(message::$ERROR_CODE, message::$ILLEGAL_REQUEST);
        }
        $file_data = [];
        $path = 'storage/';
        //获取文件名称
        $file_name = $file->getOriginalName();
        //移动文件
        $save_name = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
        $newPath = config('file.url') . '/' . $path . $save_name;
        $file_data = ['file_name' => $file_name, 'path' => $newPath];
        return ReJson(message::$SUCC_CODE, message::$UPLOADS_SUCCESS, $file_data);
    }

    public function handleImgs($files = [])
    {
        $file_name = '';
        $path = '';
        foreach ($files as $file) {
            $len = strlen(config('file.url'));
            $path .= substr($file['path'], $len) . ',';
            $file_name .= $file['file_name'] . ',';
        }
        $file_name = rtrim($file_name, ',');
        $path = rtrim($path, ',');
        return [
            'file_name' => $file_name,
            'path' => $path
        ];

    }

    /**
     * 校验json字符串
     * @param string $stringData
     * @return bool
     */
    function isJsonString($stringData)
    {
        if (empty($stringData)) return false;

        try {
            //校验json格式
            json_decode($stringData, true);
            return JSON_ERROR_NONE === json_last_error();
        } catch (\Exception $e) {
            return false;
        }
    }

    public function handle($files, $file_path, $file_name)
    {
        $array = [];
        $array[$file_path] = '';
        $array[$file_name] = '';
        if (!empty($files)) {
            //判断文件是否是json字符串
            if ($this->isJsonString($files)) {
                $files = json_decode($files, true);
            }
            $annex = $this->handleImgs($files);
            $array[$file_path] = $annex['path'];
            $array[$file_name] = $annex['file_name'];
        }
        return $array;
    }

    public function checkMoreArray($data)
    {

        if (count($data) != count($data, 1)) {
            return true;
        } else {
            return false;
        }
    }

    public function objectToArray($data)
    {
        if (is_object($data)) {
            $data = json_decode($data, true);
        }
        return $data;
    }

    /**
     * @param $arr //数据
     * @param $file_name_array // 文件字段数组
     * @param $is_two_array // 数据是否为二维数组
     * @return mixed
     */
    public function handleList($arr, $file_name_array = [], $is_two_array = false)
    {
        if (!$file_name_array) {
            return $arr;
        }
        if (!$is_two_array) {
            $arr = $this->objectToArray($arr);
            //不是二维数组
            foreach ($file_name_array as $key => $value) {
                if (isset($arr[$value])){
                    if ($arr[$value]) {
                        $path_arr = explode(',', $arr[$value]);
                        $file_name_arr = explode(',', $arr[$value . '_name']);
                        $arr[$value] = [];
                        if ($path_arr) {
                            foreach ($path_arr as $k => $v) {
                                $arr[$value][] = [
                                    'file_name' => array_key_exists($k, $file_name_arr) ? $file_name_arr[$k] : '',
                                    'path' => $v,
                                ];
                            }
                        }
                    } else {
                        $arr[$value] = [];
                    }
                }else{
                    $arr[$value] = [];
                }
            }

        } else {
            //是二维数组
            foreach ($arr as &$vv) {
                $vv = $this->objectToArray($vv);
                foreach ($file_name_array as $key => $value) {
                    if (isset($vv[$value])){
                        if ($vv[$value]) {
                            $path_arr = explode(',', $vv[$value]);
                            $file_name_arr = explode(',', $vv[$value . '_name']);
                            $vv[$value] = [];
                            if ($vv) {
                                foreach ($path_arr as $k => $val) {
                                    $vv[$value][] = [
                                        'file_name' => array_key_exists($k, $file_name_arr) ? $file_name_arr[$k] : '',
                                        'path' => $val,
                                    ];
                                }
                            }
                        } else {
                            $vv[$value] = [];
                        }
                    }else{
                        $vv[$value] = [];
                    }
                }
            }
        }
        return $arr;
    }

    /**
     * 在没有获取器的前提下进行处理前缀
     * @param $arr
     * @param $filed
     * @return mixed
     */
    public function handleUrl($arr, $filed = [])
    {
        if (empty($filed)) return $arr;

        foreach ($filed as $value) {
            foreach ($arr[$value] as &$val) {
                $val['path'] = config('file.url') . $val['path'];
            }
        }
        return $arr;
    }

    public function removeUrl($data)
    {
        if (empty($data)) return $data;
        foreach ($data as $key => &$value) {
            $len = strlen(config('file.url'));
            $value['path'] = substr($value['path'], $len);
        }
        return $data;
    }

    public function spliceFileName($data)
    {
        if (empty($data)) return $data;
        $str = '';
        foreach ($data as $key => &$value) {
            if (is_object($value)) $value = (array)$value;
            $str .= $value['file_name'] . ',';
        }
        $str = rtrim($str, ',');
        return $str;
    }

    public function spliceCheckBoxName($data)
    {
        if (empty($data)) return $data;
        $str = '';
        foreach ($data as $key => &$value) {
//            if (is_object($value)) $value = (array)$value;
            $str .= $value . ',';
        }
        $str = rtrim($str, ',');
        return $str;
    }

    public function spliceUrl($data)
    {
        if (empty($data)) return $data;
        foreach ($data as $key => &$value) {
            if (is_object($value)) $value = (array)$value;
            $value['path'] = config('file.url') . $value['path'];
        }
        return $data;
    }


}